﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Sharp_Explorer
{
    
    public partial class ImageViewer : Form
    {
        int Mouse_Y;
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;
        bool alreadyShownControl;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        public ImageViewer()
        {
            InitializeComponent();
        }

        public ImageViewer(string imagePath)
        {
            InitializeComponent();
            Image img = Image.FromFile(imagePath);
            pictureBox1.Image = img;
            pictureBox1.Height = img.Height;
            pictureBox1.Width = img.Width;
            this.Width = img.Width;
            this.Height = img.Height;
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }

            Mouse_Y = e.Y;
            if (Mouse_Y < 30)
            {
                ShowFormControls();
            }

        }

        private void ShowFormControls()
        {
            if (alreadyShownControl != true)
            {
                this.Height = pictureBox1.Height + 30;
                pictureBox1.Top = 30;
                this.Top = this.Top - 30;
                alreadyShownControl = true;
                tmrControls.Enabled = true;
                tmrControls.Interval = 500;
            }
            
        }

        private void HideFormControls()
        {
            pictureBox1.Top = 0;
            this.Top = this.Top + 30;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tmrControls_Tick(object sender, EventArgs e)
        {
            if (Mouse_Y > 30)
            {
                HideFormControls();
                tmrControls.Enabled = false;
                alreadyShownControl = false;
            }
        }

        private void ImageViewer_Load(object sender, EventArgs e)
        {
            HideFormControls();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
    }
}
